package com.example.sys.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.config.ResponseBean;
import com.example.config.UnicomResponseEnums;
import com.example.sys.entity.DictType;
import com.example.sys.service.DictTypeService;
import com.example.sys.vo.DictTypeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author anan
 * @since 2019-06-20
 */
@RestController
@RequestMapping("/sys/dict-type")
public class DictTypeController {

    @Autowired
    private DictTypeService dictTypeService;

    @GetMapping("/getDictTypeList")
    public ResponseBean<UnicomResponseEnums> getDictTypeList(DictTypeVo dictTypeVo){
        System.out.println(dictTypeVo);
        Map<String, Object> stringObjectMap = BeanUtil.beanToMap(dictTypeVo, false, true);
        if (stringObjectMap.isEmpty()){
            List<DictType> dictTypes=dictTypeService.list();
            return new ResponseBean(true, dictTypes, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, dictTypeService.selectPage(dictTypeVo), UnicomResponseEnums.SUCCESS_OPTION);
    }

    @PostMapping("/addDictType")
    public ResponseBean<UnicomResponseEnums> addDictType(@RequestBody  DictType dictType){
        if (!dictType.toString().isEmpty()) {
            dictTypeService.save(dictType);
            return new ResponseBean(true, UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(false, UnicomResponseEnums.OPERATION_FAILED);
    }

    @PostMapping("/updateDictTypeByDictId")
    public ResponseBean<UnicomResponseEnums> updateDictTypeByDictId(@RequestBody @Valid DictType dictType){
        if (dictTypeService.updateById(dictType)){
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }

    @DeleteMapping("/deleteByDictId")
    public ResponseBean<UnicomResponseEnums> deleteByDictId(@RequestBody DictTypeVo dictTypeVo){
        if (dictTypeService.removeById(dictTypeVo.getDictId())){
            return new ResponseBean(true,UnicomResponseEnums.SUCCESS_OPTION);
        }
        return new ResponseBean(true, UnicomResponseEnums.OPERATION_FAILED);
    }


}
